package com.gzcstec.service;

import com.gzcstec.dao.HostStatDao;
import com.gzcstec.domain.HostStat;
import com.gzcstec.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 *站点统计业务实现类
 */
@Service
public class HostStatServiceImpl implements HostStatService {

    @Autowired
    private HostStatDao hostStatDao;


    @Override
    public HostStat getStatByOpenIdAndHostId(String openId , String hostId) {
        return hostStatDao.getStatByOpenIdAndHostId(openId , hostId);
    }

    @Override
    public int addHostStat(HostStat hostStat) {
        hostStat.setCount(1);
        hostStat.setStatId(UUIDUtils.getUUID());
        return hostStatDao.addHostStat(hostStat);
    }

    @Override
    public int incHostStat(String statId) {
        if(StringUtils.isEmpty(statId)) {
            return 0;
        }
        return hostStatDao.incHostStat(statId);
    }

    @Override
    public int updateHostStat(String openId , String hostId) {
        //判断openId是否存在
        HostStat hs = this.getStatByOpenIdAndHostId(openId , hostId);
        if(null != hs) {
            //更新统计信息
            this.incHostStat(hs.getStatId());
        }else {
            //插入统计信息
            HostStat hs2 = new HostStat();
            hs2.setHostId(hostId);
            hs2.setOpenId(openId);
            this.addHostStat(hs2);
        }
        return 0;
    }
}
